# -*- coding: utf-8 -*-

import os,stat,time

import os2
from ps.core.path import Path

class FileInfo(object):
    def __init__(self,name):
        self.name=name
        self.exists=False
        self.modifyTime=-1
        self.size=-1
        self.isFile=False
        self.isDir=False
        
class FileStore(object):
    def __init__(self,path):
        '''__init__(Path path)'''
        
        self._path=path
        
    def getChildNames(self):
        '''abstract getChildNames() -> list
        
        return names string list
         
         '''
        raise NotImplementedError()
    
    def createFile(self,fileContent,modifyTime):
        '''abstract createFile(basestring fileContent,long modifyTime) '''
        raise NotImplementedError()
    
    def makedir(self):
        '''abstract makedir()'''
        raise NotImplementedError()
    
    #abstract
    def fetchInfo(self): 
        '''abstract fetchInfo() -> FileInfo'''
        raise NotImplementedError()
    
    #abstract
    def getName(self):
        '''getName() -> str'''
        return self._path.getLastSegment()
    
    def getChildren(self):
        '''getChildren() -> list
        
        return children FileStore list'''
        return [self.getChild(name) for name in self.getChildNames()]

    def getChild(self,name):
        '''getChild(basestring name) -> FileStore'''
        return type(self)(self._path.append(name))
    
    def getParent(self):
        '''getParent() -> FileStore
        if has parent :
           return parent 
        else:
           return None
        '''
        parentPath=self._path.getParent()
        return None if parentPath is None else type(self)(parentPath)

class LocalFileStore(FileStore):
    
    def getChildNames(self):
        '''override'''
        return os.listdir(self._getLocalPath())
    
    def createFile(self,fileContent,modifyTime=time.time()):
        '''override'''
        self.getParent().makedir()

        try:
            f=file( self._getLocalPath(), 'wb+' )
            f.write( fileContent )
        except Exception ,e: 
            raise Exception,"createFile %s\n inner exception:%s" %(self._path,e)
        finally:
            if f !=None :f.close()
            
        os2.setFileModifyTime(self._getLocalPath(), modifyTime) 


    def makedir(self):
        '''override'''
        if not os.path.exists(self._getLocalPath()):
            os.makedirs(self._getLocalPath())

    def fetchInfo(self): 
        '''override'''
        result= FileInfo(self.getName())
        if not os.path.exists(self._getLocalPath()):
            result.exists=False
            return result
        
        #now ,exists
        result.exists=True
        
        if os.path.isdir(self._getLocalPath()):
            result.isFile =False
            result.isDir =True
            return result
        
        #now,it is file:
        if os.path.isfile(self._getLocalPath()):
            result.isFile =True
            result.isDir =False
            fileStats = os.stat ( self._getLocalPath() )
            result.size = fileStats [stat.ST_SIZE]
            result.modifyTime =  fileStats [stat.ST_MTIME]

        return result

    def _getLocalPath(self):
        return str(self._path)
    
def getFileStore(path):
    return LocalFileStore(path)